//
//  WeatherCollectionViewCell.m
//  ToWhere
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "WeatherCollectionViewCell.h"
#import <MJRefresh.h>
#import "DataAnalysis.h"
#import "WeatherModel.h"
#import "WeatherTableViewCellOne.h"
#import "WeatherTableViewCellTwo.h"
#import "WeatherModelThree.h"
#import "WeatherModelTwo.h"
#import "WeatherTableViewCellThree.h"
#import "WeatherTableViewCellFour.h"
@interface WeatherCollectionViewCell ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) MJRefreshNormalHeader *header;

@property (nonatomic, retain) WeatherModel *weather;

@property (nonatomic, retain) NSMutableArray *weatherTwo;

@property (nonatomic, retain) NSMutableArray *weatherThree;

@end
@implementation WeatherCollectionViewCell

- (void)dealloc
{
    [_tableView release];
    [_header release];
    [_weather release];
    [_weatherTwo release];
    [_weatherThree release];
    [_url release];
    Block_release(_block);
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        
        self.weatherThree = [NSMutableArray array];
        self.weatherTwo = [NSMutableArray array];
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, (667 - 64) *kHeight) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.bounces = NO;
        self.tableView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0];
        
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        self.tableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
        [self.contentView addSubview:self.tableView];
    }
    self.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(Refresh)];
    
    self.header.lastUpdatedTimeLabel.hidden = YES;
    
    self.tableView.header = self.header;
    return self;
}
- (void)setUrl:(NSString *)url{
     [self.tableView.header beginRefreshing];
    if (_url != url) {
        [_url release];
        _url = [url copy];
       
    }
}
- (void)Refresh{

    [self data];
    [self.tableView reloadData];
}
- (void)data{

    NSString *urlString = [NSString stringWithFormat:@"http://tq.91.com/api/?act=107&pid=115&word=%@ &osv=8.1&dm=iPhone5,3&chl=otSy9Zonl/k5CTkt33reOQ==&sv=3.13.0&spid=1&nt=10&imei=D3BF9AF1-1DF9-4B08-86CB-F4CB972E3DF0&mt=1",self.url];
    
   NSString *beijingString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [DataAnalysis dataAnalysisGETWithUrlString:beijingString parameters:nil showHuD:NO hiddenView:nil successfulBlock:^(id responseObject)
    {
        NSDictionary *dic = responseObject;
        NSString *str = [NSString stringWithFormat:@"http://tq.91.com/api/?chl=otSy9Zonl%@&city=%@&spid=1&productid=8032&osv=8.1&imei=D3BF9AF1-1DF9-4B08-86CB-F4CB972E3DF0&cityname=%@&vercode=3.13.0&dm=%@",@"%2Fk5CTkt33reOQ%3D%3D",[[[dic objectForKey:@"items"] firstObject] objectForKey:@"id"],@"%E7%93%A6%E6%88%BF%E5%BA%97",@"iPhone5%2C3&sv=3.13.0&option=now,weather,tqzs,tqyj,sun,pm,wind&act=215&pid=115&chkcode=9c122cd2f020dd001c4e30f00c4218a3&nt=10&mt=1"];
        [DataAnalysis dataAnalysisGETWithUrlString:str parameters:nil showHuD:NO hiddenView:nil successfulBlock:^(id responseObject) {
            NSDictionary *data = responseObject;
            
            self.weather = [WeatherModel weatherWithDictionary:data];
            
            for (NSDictionary *dic in [self.weather.weatherinfo objectForKey:@"weather"]) {
                WeatherModelTwo *two = [WeatherModelTwo weatherModelTwoWithDictionary:dic];
                [self.weatherTwo addObject:two];
                
            }
            for (NSDictionary *dic in [self.weather.windinfo objectForKey:@"vecwind"]) {
                WeatherModelThree *three = [WeatherModelThree weatherModelThreeWithDictionary:dic];
                [self.weatherThree addObject:three];
            }
            
            NSDictionary *dic = [NSDictionary dictionaryWithObject:data forKey:@"weather"];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"Image" object:self userInfo:dic];
            
            [self.tableView reloadData];
        
            [self.tableView.header endRefreshing];
            
        } failureBlock:^(NSError *error) {
            
        }];
        
    } failureBlock:^(NSError *error) {
        
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        return (667 - 64) *kHeight;
    }
    else if(indexPath.section == 1){
    
        return 400 *kHeight;
    }else if (indexPath.section == 2){
    
        return 250 *kHeight;
    }
    else{
        return 100 *kHeight;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        return 0.0001;
    }
    else{
    
        return 50 *kHeight;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375 *kWidth, 20 *kHeight)];
    view.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375 *kWidth, 50 *kHeight)];
    
    view.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 375 *kWidth, 48 *kHeight)];
    label.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.35];
    label.font = [UIFont systemFontOfSize:18 *kWidth];
    label.textColor = [UIColor whiteColor];
    
    [view addSubview:label];
    [label release];

    if (section == 1) {
        label.text = @" 多日天气";
        return view;
    }
    else if (section == 2){
    
        label.text = @" 指数";
    return view;
    }
    else if (section == 3){
    
        label.text = @" 风力湿度";
        return view;
    }
    else{
    
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    if (section == 3) {
        return 0;
    }else{
    
        return 10 *kHeight;
    
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        WeatherTableViewCellOne *cell = [WeatherTableViewCellOne weatherTableViewCellOneWithTableView:tableView];
        cell.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.weather = _weather;
        
        return cell;

    }else if (indexPath.section == 1){
    
        WeatherTableViewCellTwo *cell = [WeatherTableViewCellTwo weatherTableViewCellTwoWithTableView:tableView];
        cell.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.weatherTwo.count == 0 || self.weatherThree.count == 0) {
            return cell;
        }
        cell.weatherTwoArray = self.weatherTwo;
        cell.weatherThreeArray = self.weatherThree;
        return cell;
        
    }else if (indexPath.section == 2){
    
        WeatherTableViewCellThree *cell = [WeatherTableViewCellThree weathertableViewCellThreeWithTableView:tableView];
        cell.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dic = self.weather.tqzs;
        return cell;
    
    }else{
    
        WeatherTableViewCellFour *cell = [WeatherTableViewCellFour weatherTableViewCellFourWithTableView:tableView];
        cell.dic = self.weather.now;
        cell.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    self.block(self.tableView.contentOffset.y,[self.weather.now objectForKey:@"temp"]);
}
@end
